<template>
  <div class="login">
    <div class="box">
      <div class="title">
        <div class="left">
          <div class="img" />
          <span>黑马面面</span>
        </div>
        <div class="right">
          <span>用户登陆</span>
        </div>
      </div>
      <div class="form">
        <el-form ref="dataForm" :model="dataForm" :rules="rules">
          <el-form-item prop="phone">
            <el-input
              v-model="dataForm.phone"
              prefix-icon="el-icon-user"
              placeholder="请输入手机号"
              style="width: 394px; height: 45px"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="dataForm.password"
              prefix-icon="el-icon-lock"
              show-password
              placeholder="请输入密码"
              style="width: 394px; height: 45px"
            />
          </el-form-item>
          <el-form-item prop="code">
            <el-input
              v-model="dataForm.code"
              prefix-icon="el-icon-key"
              placeholder="请输入验证码"
              style="width: 284px; height: 45px"
            />
            <img v-if="bol" :src="imgUrl" alt="" @click="refreshImg">
          </el-form-item>
          <el-form-item prop="checked" style="margin-bottom: 0">
            <el-checkbox
              v-model="dataForm.checked"
            >我已阅读并同意<span>用户协议</span>和<span>隐私条款</span>
            </el-checkbox>
            <!-- <a @click="$router.push('/register')">注册账号</a> -->
          </el-form-item>
          <el-form-item>
            <div style="font-size: 12px">
              还没有账号?去<router-link
                :to="{ path: '/register' }"
                tag="a"
                style="color: #409eff"
              >注册</router-link>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              style="width: 394px; height: 40px"
              @click="submit"
            >登陆</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="bigImg" />
  </div>
</template>
<script>
export default {
  name: '',
  data() {
    return {
      bol: true,
      t: 0,
      baseUrl: process.env.VUE_APP_URL,
      dataForm: {
        phone: '',
        password: '',
        code: '',
        checked: false
      },
      rules: {
        phone: [
          { required: true, message: '请输入正确的手机号', trigger: 'change' },
          {
            pattern:
              /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正确的手机号',
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          { min: 6, max: 12, message: '请输入密码', trigger: 'change' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'change' },
          { max: 4, min: 4, message: '请输入验证码', trigger: 'change' }
        ],
        checked: [
          {
            validator: (rule, value, callback) => {
              value === true ? callback() : callback(new Error('请勾选协议'))
            }
          }
        ]
      }
    }
  },
  computed: {
    imgUrl() {
      return this.baseUrl + 'captcha?type=login&time=' + this.t
    }
  },
  create() {},
  methods: {
    refreshImg() {
      // this.bol = false
      // this.$nextTick(() => {
      //   this.bol = true
      // })
      this.t = new Date().getTime()
    },
    submit() {
      this.$refs.dataForm.validate(async(result) => {
        if (result) {
          const res = await this.$api.system.loginPost(this.dataForm)
          this.$store.commit('user/SET_TOKEN', res.data.token)
          this.$message.success('登陆成功')
          this.$router.push('/')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.login {
  background-color: #119bfa;
  min-height: 100vh;
  overflow: hidden;
  display: flex;
  position: relative;
  .bigImg {
    position: absolute;
    right: 73px;
    top: 234px;
    width: 633px;
    height: 435px;
    background-image: url(/static/img/login_banner_ele.c304439e.png);
    background-position: center right;
    background-size: 100%;
    background-repeat: no-repeat;
  }
  .box {
    margin-top: 179px;
    margin-left: 360px;
    background-color: #fff;
    width: 478px;
    height: 500px;
    padding: 48px;
    z-index: 9999;
    .title {
      display: flex;
      .left {
        display: flex;
        align-items: center;
        border-right: 1px solid #ccc;
        .img {
          margin-right: 16px;
          width: 22px;
          height: 17px;
          background: url('~@/assets/common/login_logo.png') center center /
            cover;
        }
        span {
          margin-right: 14px;
        }
      }
      .right {
        flex: 1;
        margin-left: 11px;
      }
      font-size: 24px;
      margin-bottom: 27px;
    }
    .form {
      span {
        color: #409eff;
        &:hover {
          text-decoration: underline;
        }
      }
      .register {
        margin-left: 15px;
        color: #119bfa;
      }
      ::v-deep .el-form-item__content {
        display: flex;
        img {
          width: 110px;
          height: 42px;
        }
      }
    }
  }
}
</style>
